<template>
    <!-- 修改资料 -->

    <div>
        <el-form label-width="85px" :model="form" label-position="right" ref="formRef" :rules="formRules">
            <el-form-item label="编号" prop="number">
                <el-input v-model="form.number"></el-input>
            </el-form-item>
            <el-form-item label="名称" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="角色类型" prop="roleKind">
                <el-select v-model="form.roleKind" clearable placeholder="请选择：">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="模块集合" prop="moduleIds">
                <el-tree ref="moduleRef" :data="moduleData" multiple check-strictly :render-after-expand="false"
                    node-key="value" show-checkbox check-on-click-node :expand-on-click-node="false"
                    :default-checked-keys="form.moduleIds" @node-click="nodeClick" @check="nodeCheck" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-switch v-model="form.status" size="large" inline-prompt :active-icon="Check" :inactive-icon="Close"
                    active-value="normal" inactive-value="disable" />
            </el-form-item>
            <el-form-item label="显示顺序" prop="itemIndex">
                <el-input-number v-model="form.itemIndex" :min="0" />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark"></el-input>
            </el-form-item>
        </el-form>
        <div class="button-position">
            <el-button @click="updateVisible">取 消</el-button>
            <el-button type="primary" @click="updateRole(formRef)">确 定</el-button>
        </div>
    </div>
</template>

<script setup>
import { reactive, ref, getCurrentInstance, onMounted } from 'vue';
import { ElNotification } from 'element-plus';
import Api from '@/api/model/system'
const emit = defineEmits(['updatetable'])


const props = defineProps({
    paramsData: {
        type: Object,
        defalut: {}
    }
})
// 添加用户数据的对象
const form = ref({

});

const formRef = ref()
const formRules = reactive({
})
const options = ref()
const moduleData = ref()
const moduleRef = ref(null)
onMounted(() => {
    getInit()
    getData()
})

const getInit = async () => {
    let params = {
        fileName: 'SysMenuTreeReport'
    }
    let treeRes = await Api.menu.moduleTree.post(params)
    moduleData.value = treeRes.reportData
    let listRes = await Api.role.add.post()
    options.value = listRes.configData.roleKindList
}

const getData = async () => {
    let res = await Api.role.detail.post(props.paramsData);
    if (res.code == 200) {
        form.value = res.data;
    } else {
        form.value = '';
    }

}
const nodeClick = (data, node) => {
    if (node.checked == true) {
        childNodesChange(node);
        parentNodesChange(node);
    }
}
const nodeCheck = (data) => {
    let node = moduleRef.value.getNode(data.value)
    if (node.checked == true) {
        childNodesChange(node);
        parentNodesChange(node);
    }
}
const childNodesChange = (node) => {
    let len = node.childNodes.length;
    if (len > 0) {
        for (let i = 0; i < len; i++) {
            console.log(node.checked)
            if (node.checked) {
                node.childNodes[i].checked = true;
            }
            childNodesChange(node.childNodes[i]);
        }
    }
}
const parentNodesChange = (node) => {
    console.log(node)
    if (node.parent) {
        for (let key in node) {
            if (key == "parent") {
                node[key].checked = true;
                parentNodesChange(node[key]);
            }
        }
    }
}


const updateVisible = () => {
    emit('updatetable')
}



const restupdateFormData = () => {
    formRef.value.resetFields();
}



// 上传
const updateRole = async (formRef) => {
    await formRef.validate((valid, fields) => {
        if (valid) {
            updateRoleData();
        }
    })
}

const updateRoleData = async () => {
    var params = {
        bean: form.value,
    }
    let moduleArr = []
    moduleRef.value.getCheckedNodes(false).forEach(ele => {
        moduleArr.push(ele.value)
    });
    form.value.moduleIds = moduleArr
    var res = await Api.role.update.post(params);
    if (res.code == 200) {
        restupdateFormData()
        ElNotification.success({ title: '', message: '修改成功！', duration: 2000 });
        emit('updatetable', true)
    }
}

</script>

<style lang="scss" scoped></style>

